package com.yiyahui.project.erp.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.yiyahui.project.erp.domain.YiyaClinicPerson;
import com.yiyahui.project.erp.domain.requestVo.SearchRequest;
import com.yiyahui.project.erp.domain.requestVo.YiYaMarketRequest;
import com.yiyahui.project.erp.service.IYiyaClinicPersonService;
import com.yiyahui.project.erp.service.IYiyaIDService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yiyahui.framework.aspectj.lang.annotation.Log;
import com.yiyahui.framework.aspectj.lang.enums.BusinessType;
import com.yiyahui.project.erp.domain.YiyaMarketReservation;
import com.yiyahui.project.erp.service.IYiyaMarketReservationService;
import com.yiyahui.project.erp.service.IYiyaOperationLogService;
import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.common.utils.poi.ExcelUtil;
import com.yiyahui.framework.web.page.TableDataInfo;

/**
 * 市场建档Controller
 * 
 * @author yiyahui
 * @date 2020-05-03
 */
@RestController
@RequestMapping("/erp/reservation")
public class YiyaMarketReservationController extends BaseController
{
    @Autowired
    private IYiyaMarketReservationService yiyaMarketReservationService;

    @Autowired
    private IYiyaOperationLogService yiyaOperationLogService;

    @Autowired
    private IYiyaIDService yiyaIDService;

    @Autowired
    private IYiyaClinicPersonService yiyaClinicPersonService;

    /**
     * 查询市场建档列表
     */
    @PreAuthorize("@ss.hasPermi('erp:reservation:list')")
    @GetMapping("/list")
    @ApiOperation("市场建档列表查询")
    public TableDataInfo list(YiYaMarketRequest yiYaMarketRequest)
    {
        startPage();
        List<YiyaMarketReservation> list = yiyaMarketReservationService.selectYiyaMarketReservationList(yiYaMarketRequest);
        return getDataTable(list);
    }

    /**
     * 导出市场建档列表
     */
    @PreAuthorize("@ss.hasPermi('erp:reservation:export')")
    @Log(title = "市场建档", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("市场建档列表导出")
    public AjaxResult export(YiYaMarketRequest yiYaMarketRequest)
    {
        List<YiyaMarketReservation> list = yiyaMarketReservationService.selectYiyaMarketReservationList(yiYaMarketRequest);
        ExcelUtil<YiyaMarketReservation> util = new ExcelUtil<YiyaMarketReservation>(YiyaMarketReservation.class);
        return util.exportExcel(list, "reservation");
    }

    /**
     * 获取市场建档详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp:reservation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        yiyaOperationLogService.insertDailyBrowserLog(0,id);
        return AjaxResult.success(yiyaMarketReservationService.selectYiyaMarketReservationById(id));
    }

    /**
     * 新增市场建档
     */
    @PreAuthorize("@ss.hasPermi('erp:reservation:add')")
    @Log(title = "市场建档", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YiyaMarketReservation yiyaMarketReservation)
    {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.setKeyword(yiyaMarketReservation.getPhoneNumber());
        List<YiyaClinicPerson> yiyaClinicPersons = yiyaClinicPersonService.selectByNameOrPhone(searchRequest);
        List<String> caseNums = yiyaClinicPersons.stream().map(YiyaClinicPerson::getCardNumber).collect(Collectors.toList());
        List<YiyaMarketReservation> yiyaMarketReservations = yiyaMarketReservationService.selectByNameOrPhone(searchRequest);
        List<YiyaMarketReservation> result = yiyaMarketReservations.stream().filter(r -> !caseNums.contains(r.getCaseNumber())).collect(Collectors.toList());
        int count = result.size() + yiyaClinicPersons.size();
        if (count >= 3) {
            return AjaxResult.error("建档号码超过3个");
        } else {
            yiyaMarketReservation.setReservationNumber(yiyaIDService.createYiyaID(IYiyaIDService.IDType_JIANDANGHAO,yiyaMarketReservation.getFilingPerson() != null ? yiyaMarketReservation.getFilingPerson() : ""));
            yiyaMarketReservation.setReach(yiyaMarketReservation.getReach() != null && !"".equals(yiyaMarketReservation.getReach()) ? yiyaMarketReservation.getReach() : " ");
            yiyaMarketReservation.setRecord(yiyaMarketReservation.getRecord() != null && !"".equals(yiyaMarketReservation.getRecord()) ? yiyaMarketReservation.getRecord() : " ");
            yiyaMarketReservation.setDetails(yiyaMarketReservation.getDetails() != null && !"".equals(yiyaMarketReservation.getDetails()) ? yiyaMarketReservation.getDetails() : " ");
            return toAjax(yiyaMarketReservationService.insertYiyaMarketReservation(yiyaMarketReservation));
        }

    }

    /**
     * 修改市场建档
     */
    @PreAuthorize("@ss.hasPermi('erp:reservation:edit')")
    @Log(title = "市场建档", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YiyaMarketReservation yiyaMarketReservation)
    {
        return toAjax(yiyaMarketReservationService.updateYiyaMarketReservation(yiyaMarketReservation));
    }

    /**
     * 删除市场建档
     */
    @PreAuthorize("@ss.hasPermi('erp:reservation:remove')")
    @Log(title = "市场建档", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yiyaMarketReservationService.deleteYiyaMarketReservationByIds(ids));
    }
}
